/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.miscellaneous.map;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import twaver.TWaverUtil;
import twaver.swing.TableLayout;

public class MapControlPanel extends JPanel {

	private MapDemo mapDemo;

	public MapControlPanel(MapDemo mapDemo) {
		this.mapDemo = mapDemo;
		initGUI();
	}

	private void initGUI() {
		double cols[] = { TableLayout.PREFERRED, TableLayout.FILL };
		double rows[] = { TableLayout.FILL, TableLayout.FILL, TableLayout.FILL, TableLayout.FILL };
		TableLayout layout = new TableLayout(cols, rows, 5, 5);
		final JPanel panel = new JPanel(layout);

		final JComboBox strokeComboBox = new JComboBox();
		final JTextField renderField = createTextFiele(mapDemo.getRenderColor());
		final JTextField outLineField = createTextFiele(mapDemo.getOutlineColor());
		final JCheckBox checkBox = new JCheckBox("Editable");
		checkBox.setBackground(new Color(255, 250, 250, 0));
		checkBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mapDemo.setEditable(checkBox.isSelected());
			}
		});
		panel.add(checkBox, "0,0,0,0");

		JButton button = new JButton("Reload");
		button.setContentAreaFilled(false);
		button.setOpaque(false);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mapDemo.loadXML();
				mapDemo.setRenderColor(renderField.getForeground());
				mapDemo.setOutlineColor(outLineField.getForeground());
				mapDemo.setOutlineStroke(strokeComboBox.getSelectedIndex());
				mapDemo.setEditable(checkBox.isSelected());
			}
		});
		panel.add(button, "1,0,1,0");

		panel.add(new JLabel("Fill Color"), "0,1,0,1");
		panel.add(renderField, "1,1,1,1");
		renderField.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				if ("foreground".equals(evt.getPropertyName())) {
					mapDemo.setRenderColor(renderField.getForeground());
				}
			}
		});

		panel.add(new JLabel("OutLine Color"), "0,2,0,2");
		outLineField.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				if ("foreground".equals(evt.getPropertyName())) {
					mapDemo.setOutlineColor(outLineField.getForeground());
				}
			}
		});

		panel.add(outLineField, "1,2,1,2");

		panel.add(new JLabel("OutLine Stroke"), "0,3,0,3");
		DefaultListCellRenderer renderer = new DefaultListCellRenderer();
		renderer.setOpaque(false);
		strokeComboBox.setRenderer(renderer);
		for (int i = 0; i < 10; i++) {
			strokeComboBox.addItem("" + i);
		}
		strokeComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mapDemo.setOutlineStroke(strokeComboBox.getSelectedIndex());
			}
		});
		strokeComboBox.setSelectedIndex(1);
		strokeComboBox.setOpaque(false);
		panel.add(strokeComboBox, "1,3,1,3");

		panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		panel.setOpaque(false);
		this.setBackground(new Color(200, 200, 200, 100));
		this.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
		this.setLayout(new BorderLayout());
		this.add(panel, BorderLayout.CENTER);
	}

	private JTextField createTextFiele(Color color) {
		final JTextField field = new JTextField(8);
		field.setBackground(new Color(200, 200, 200, 150));
		field.setBorder(BorderFactory.createRaisedBevelBorder());
		field.setEditable(false);
		field.setText(TWaverUtil.getColorHexString(color, true));
		field.setForeground(color);
		field.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				Color selectedColor = JColorChooser.showDialog(TWaverUtil.getWindowForComponent(field), "Color", field.getForeground());
				if (selectedColor != null) {
					String colorString = TWaverUtil.getColorHexString(selectedColor, true);
					field.setText(colorString);
					field.setForeground(selectedColor);
					field.repaint();
				}
			}
		});
		return field;
	}
}